package Controller.UserController;

import Utils.BaseMessage;
import Utils.isString;
import ViewOption.userVo;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 修改个人信息，state代表修改密码或者修改个人信息
 */
@WebServlet(name = "UpdateUserMsgServlet", urlPatterns = {
        "/api/User/isLogin/UpdateUserMsgServlet"
})
public class UpdateUserMsgServlet extends HttpServlet {
    private final userVo userVo = new userVo();
    private final isString isString = new isString();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String state = request.getParameter("state");//判断是修改密码还是修改信息
        if (state == null) {
            response.getWriter().print(JSON.toJSONString(new BaseMessage(2, "非法传参", null)));
            return;
        }
        if (state.equals("1")) {//修改密码
            String oldPassword = request.getParameter("oldPassword");
            String newPassword = request.getParameter("newPassword");
            if (isString.IsEmpty(oldPassword, newPassword)) {
                response.getWriter().print(JSON.toJSONString(new BaseMessage(2, "非法传参", null)));
                return;
            }
            response.getWriter().print(JSON.toJSONString(userVo.updatePasswordByUserId(request)));
            return;
        } else if (state.equals("2")) {
            String nickname = request.getParameter("nickname");
            String email = request.getParameter("email");
            String username = request.getParameter("username");
            if (isString.IsEmpty(nickname, email, username)) {
                response.getWriter().print(JSON.toJSONString(new BaseMessage(2, "非法传参", null)));
                return;
            }
            response.getWriter().print(JSON.toJSONString(userVo.updateUserMsgByUserId(request)));
            return;
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
